After synchronous request response, it's now time to discuss asynchronous
message passing using Pub/Sub. Pub/Sub is an asynchronous
messaging service. This is the runtime model of Pub/Sub
a publisher sends a message to a topic on Pub/Sub, Pub/Sub,
then copies the message and guarantees delivering to one or
more subscribers. Both the publisher and the subscriber
can be a cloud run service but that's not a requirement a publisher
can be any process that has access to the Pub/Sub API. A subscriber can be any https endpoint or
a process with access to the Pub/Sub API another key property
of Pub/Sub is that the publishers are decoupled from the subscribers
the publisher is not waiting for confirmation and there's no need for
a read receipt by design. One pub sub topic can have multiple
subscribers each subscriber is configured separately using the subscription resource
included in the subscription setting are for example the https endpoint to
send messages to the retry configuration if messages should be delivered in
order or potentially out of order, which decreases latency or
whether to apply a filter two messages. All of these settings are unique and
can be set independently for every subscription in the setup
to allow to cloud run services to communicate through Pub/Sub
this is what you'll need to do. First, you'll need to create a topic
that the publishing cloud run service can send messages to, second you'll
need to create a push subscription and attach it to the topic and third you
need to configure the push subscription to forward messages to the https URL
of the subscribing cloud run service. In the runtime section if you set
up publishing and a subscriber, this is what happens when
the publisher sends a message. First the publisher publishes a message
to a topic where the message queues, second the pub/sub copies the message to
all subscriptions that are attached to the topic and third the push subscription
forwards the message to the subscribing cloud run service it puts the message
in the body of the http post request. There are many communication styles
supported by cloud pub/sub and I want to quickly walk you through
them starting with one too many. You can have one topic with
multiple subscribers as shown here where one topic is pushed
to multiple URLs thereby one topic is pushing subscriptions
out to two different URLs. The inverse is also supported multiple
publishers can post to a single topic and a single push subscription
handles the events an example use case is where multiple services
are publishing events and one service exists to aggregate
all events in a data store. Using push subscriptions there
is an alternative pattern you can have two topics
subscription pairs, push messages to the same HTTPS URL
this is useful if you want to send messages to the same service but
two different paths or have a different retry configuration for
different publishers. And finally you can combine many to
one and want to many in one pattern or many to many a use case where all
services post and read events to and from a shared channel where multiple
message filtering happens on the services. Finally, and I like to explicitly
call out this pattern Pub/sub also supports direct invocation
where you can create a topic and a subscription for two services that
want to communicate with one another. One of the benefits of using Pub/sub
over sending a direct message is that the publisher can send
the message to pub/sub and forget about it the publisher doesn't
need to wait for the result and handle errors with re tries even if
the subscriber is temporarily unavailable Pub/subs still guarantees that the message
will be delivered at least once. Let's take a look at how pub/sub decides
if a message delivery is successful. To deliver a message to a subscriber
Pub/sub sends the request if the request, times out it was retry the request if
the subscriber returns a response, it re tries the request if
the response is not successful, the default deadline is 10 seconds,
which can be increased up to 10 minutes. Pub/sub decides if a request is
successful by looking at the status code returned this can be in
a different number of ranges for HTTP such as 1 or 2, 200, 201, 202 or 204 it's useful to further
indicate how re tries work, retries do not continue forever and
they can use exponential back off. But what exactly does that mean? Exponential back off enables pub/sub
to use a retry mechanism where pub/sub waits 10 seconds before retrying
a message if the message fails, pub/sub, increases the delay
before retrying for the next time. For every retry,
pub/sub keeps increasing the delay up to a 10 minute interval
between re tries for retention duration a subscription
tries to deliver a message for 7 days and will discard the message
if a continuation fails. But hang on,
what is this about discarding a message? What if you don't like the idea
of losing undeliverable messages? Well, you can actually save them and
prevent them from being discarded after those 7 days to save undelivered messages,
you can figure a dead letter topic a dead letter topic is a subscription
property not a topic property. To forward undeliverable messages
complete the following steps. Create a new topic one that should receive
the undeliverable messages create or update a subscription and
set the dead letter topic, you can also set the maximum number of delivery
attempts you don't have to wait 7 days. If you want to make sure a service
can only receive a message from a specific push subscription you
can use IAM to restrict access to the cloud run service the push
subscription has a service identity. To restrict access to only they push
subscription complete the following steps. Create a service account
Configure the service account and the identity of the post subscription,
create a policy binding that binds the service account to
the cloud run invoke a role and finally add the binding to the IAM policy
that attaches to the cloud run service. If you completed these four steps,
Pub/sub automatically adds a Google signed open
ID token to the request for the pub subscription to
the cloud run service and the IAM will reject all other requests. In summary Pub/sub is
an asynchronous messaging service you can publish
a message to a topic and use push subscriptions to send
messages to any HTTPS endpoint. The push subscription guarantees
at least once delivery and re tries failed messages,
to avoid discarded messages configure a dead letter topic and
monitor the messages to that topic. I also want to discuss cloud tasks
which is a cloud pub/sub alternative on Google cloud both products
are very similar but there are a few important differences conceptually
both support the same workflow. A publisher can submit a message and
both cloud tasks and pub/sub will both forward that
message to any public HTTPS endpoint both products guarantee
at least once delivery and support IAM authentication
when to choose cloud tasks. There are two important reasons why you
should choose cloud tasks over pub/sub for service to service communication if you
need any of these choose cloud tasks, explicit rate controls, longer timeouts to respond to
a message I.e greater than 10 minutes. For rate controls these
are particularly useful if you're sending messages to an external
end point that requires you do not exceed a certain request rate
threshold in requests per second. Pub/sub also adjusts the right
request based on error rates but it doesn't offer guarantees rate
limiting is useful if you want to send a request to an external API that requires
you to respect a certain rate limit or another resource with
capacity constraints. Cloud Pub/sub only supports a request
time out of up to 10 seconds Cloud tasks supports timeouts of up to 30
minutes this is useful if you want to complete a longer running tasks that can't
be split up into multiple smaller tasks. There are certain features only available
to pub/sub by default cloud tasks and Pub/sub can potentially deliver
messages out of order you can prevent that by enabling
message ordering however, only pub/sub has the option to deliver
messages in the order they were published. However, you need to be aware that using
this option will increase latency for message delivery by
default cloud tasks and cloud pub/sub both discard
undeliverable messages however, Pub/sub has the option to forward
undeliverable messages to another topic. Do note that you can't have
both message ordering and catching undeliverable messages
enabled at the same time.